//
//  LTHomeViewController.swift
//  myMoya
//
//  Created by lete on 2020/4/27.
//  Copyright © 2020 xiaoyi. All rights reserved.
//

import UIKit

class LTHomeViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var photoImgView: UIImageView!
    
    @IBOutlet weak var phoneTF: UITextField!
    @IBOutlet weak var remarkTF: UITextField!
    
    
    let radioAlbum = LTRadioAlbum()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        setupUI()
        
        phoneTF.delegate = self
        
        
        phoneTF.addTarget(self, action: #selector(textDidChange), for: .editingChanged)

        
    }
    
    
    func setupUI() {
        self.view.backgroundColor = UIColor.white
        
    }
    
    //MARK: - btn method
    
    //MJRefresh 的 demo
    @IBAction func gotoMJRefreshBtnHandle(_ sender: UIButton) {
        
        let vc = LTRecommendViewController()
        
        self.navigationController?.pushViewController(vc, animated: true)
        
        
    }
    //     多张图片选择器
    @IBAction func selectedImageBtnHandle(_ sender: UIButton) {
        
        
        lt_openAlbumServiceWithBlock { (tag) in
            if tag {
                self.gotoPhotoChooise()
            } else {
                lt_OpenURL()
            }
        }
        
        
        
        
    }
    
    func gotoPhotoChooise() {
        //开始选择照片，最多允许选择4张
        self.presentHGImagePicker(maxSelected:4) { (assets) in
            //结果处理
            print("共选择了\(assets.count)张图片，分别如下：")
            for asset in assets {
                print(asset)
                
                let image =  PHAssetToUIImage(asset: asset)
                LTLog("照片===>>> \(image)")
            }
            self.photoImgView.image = PHAssetToUIImage(asset: assets[0])
        }
    }
    
    //拍照/选择照片(单张)
    @IBAction func takeOrSelectPhotoBtnHandle(_ sender: UIButton) {
        
        
        weak var weakSelf = self // 弱引用
        radioAlbum.selectedImageBlock = { (image)in // 选择图片的回调
            LTLog("照片:\(image)")
            weakSelf!.photoImgView.image = image
        }
        
        radioAlbum.showPromptBox()
    }
    
    @IBAction func newNetworkTool(_ sender: UIButton) {
        
//        LENetwork<HomeService, HomeData>.request(target: .demo22(name: "ddd", age: 33)) { (home) in
//
//            print(home.sidebar)
//        }
        
        LENetwork<HomeService, HomeData>.request(target: .demo22(name: "erererer", age: 45)) { (dataModel, baseModel) in
             print(dataModel.sidebar)
            print(dataModel.sidebar[0].name)
            print(baseModel.text)
             print(baseModel.status)
        }
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
    }
    
   @objc func textDidChange(_ sender:UITextField) {
        LTLog("输入 ===>>>> \(String(describing: phoneTF.text))")

//    if sender == phoneTF{}
        if phoneTF.text!.count > 3 {
            let str:String = sender.text!
            
            let astr = str.prefix(3)
            phoneTF.text = String(astr)
      }
    }
   
    
}
